import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/Home.vue'),
      meta: {
        title: '小王闲谈 - 首页'
      }
    },
    {
      path: '/articles',
      name: 'articles',
      component: () => import('../views/Articles.vue'),
      meta: {
        title: '文章列表'
      }
    },
    {
      path: '/article/:id',
      name: 'article-detail',
      component: () => import('../views/ArticleDetail.vue'),
      meta: {
        title: '文章详情'
      }
    },
    {
      path: '/resources',
      name: 'resources',
      component: () => import('../views/Resources.vue'),
      meta: {
        title: '软件资源'
      }
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/About.vue'),
      meta: {
        title: '关于我们'
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFound.vue'),
      meta: {
        title: '页面未找到'
      }
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '小王闲谈'
  next()
})

export default router 